package com.snailvr.tv.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.util.Util;
import com.snail.readyvr.R;
import com.snailvr.tv.network.entity.RoomListResponBean;
import com.snailvr.tv.ui.activity.PlayActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/10/19.
 */
public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.RoomViewHolder>{
    public interface onRoomChangeListener {
        void onRoomChange(int room_id);
    }
    private Context mContext;
    private List<RoomListResponBean.DataBean.ListBean> mRooms;
    private onRoomChangeListener mListener;

    public RoomAdapter(Context _context, List<RoomListResponBean.DataBean.ListBean> roomlist, onRoomChangeListener _Listener) {
        super();
        mContext = _context;
        mRooms = roomlist;
        mListener = _Listener;
    }

    @Override
    public RoomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View _view = LayoutInflater.from(mContext).inflate(R.layout.layout_room_item, parent, false);
        final RoomViewHolder mHolder = new RoomViewHolder(_view);
        _view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onRoomChange(mRooms.get(mHolder.getAdapterPosition()).getId());
            }
        });
        return mHolder;
    }

    @Override
    public void onBindViewHolder(RoomViewHolder holder, int position) {
        RoomListResponBean.DataBean.ListBean _Bean = mRooms.get(position);
        if (Util.isOnMainThread()){
            Glide.with(mContext)
                    .load(_Bean.getThumb())
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(holder.mImageView);
        }
        holder.mTextViewName.setText(_Bean.getName());
        holder.mTextViewTime.setText(mContext.getResources().getString(R.string.desc) + ": " + _Bean.getDesc());
        if (_Bean.getId() == PlayActivity.mRoomInfoBean.getId()) {
            holder.mLayout.setBackgroundResource(R.drawable.shape_round_layout_5_blue);
            holder.mImageViewLive.setVisibility(View.VISIBLE);
            holder.mImageViewPlay.setVisibility(View.GONE);
        } else {
            holder.mImageViewLive.setVisibility(View.GONE);
            holder.mLayout.setBackgroundResource(0);
            holder.mImageViewPlay.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        return mRooms.size();
    }

    class RoomViewHolder extends RecyclerView.ViewHolder
    {
        RelativeLayout mLayout;
        ImageView mImageView;
        ImageView mImageViewPlay;
        ImageView mImageViewLive;
        TextView mTextViewName;
        TextView mTextViewTime;
        public RoomViewHolder(View _view) {
            super(_view);
            mLayout = (RelativeLayout) _view.findViewById(R.id.layout_root);
            mImageView = (ImageView)_view.findViewById(R.id.img_view_poster);
            mTextViewName = (TextView)_view.findViewById(R.id.txt_view_video_name);
            mTextViewTime = (TextView)_view.findViewById(R.id.txt_view_video_desc);
            mImageViewLive = (ImageView)_view.findViewById(R.id.img_view_live);
            mImageViewPlay = (ImageView)_view.findViewById(R.id.img_view_play_button);
        }
    }

    private static String generateTime(long utc) {
        Date mDate = new Date(utc);
        SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
         return  fmt.format(mDate);

    }
}
